var req = '';
var ajax_ready = false;

function ask( msg, func, argument )
{
	var ok = window.confirm( msg );
	if ( ok )
	{
		eval( func )( argument );
	}
}

function hide_this_cate(tips_name, cate_id,site_id)
{
	if( loading_animation(tips_name) )
	{
		ajax_ready = false;
		ajax_call('GET','hide.php?cate_id=' + cate_id + '&site_id='+site_id+'&t=' +((new Date()).getSeconds()),"hide_callback('"+tips_name+"')", '');
	}
}

function hide_callback(tips_name)
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
			clearInterval(INT);
			var result = req.responseText;
			if( result.match((new RegExp('^[0-9|]{1,}$','gi'))) )
			{
				var t = result.split('|');
				MM_findObj('L_'+t[0]+'_'+t[1]).style.display = 'none';
			}
			else
			{
				MM_findObj(tips_name).innerHTML = tips(result, 70);
			}
			ajax_ready = true;
		}
	}
}

function callme(uri, site_name)
{
	if( loading_animation('tips') )
	{
		if( ! site_name ) site_name = '浏览网站';
		MM_findObj("do").disabled = true;
		ajax_ready = false;
		MM_findObj("site").value = uri;
		//$("#main_news_block > ul").tabs("enable",3);
		$("#main_news_block > ul").tabs("select",3);
		MM_findObj('site_name').innerHTML = site_name;
		if( uri.substr(0,4) != 'http' )
		{
			uri = 'http://' + uri + '/';
			MM_findObj("site").value = uri;
		}
		if( uri.substr( uri.length-1,1) == '#' )
		{
			uri = uri.substr( 0, uri.length-1 );
			MM_findObj("site").value = uri;
		}
		MM_findObj('external_link').href = uri;
		ajax_call("GET","list.php?uri=" + uri + '&t=' +((new Date()).getSeconds()),"altermsg('tips')", '');
	}
	return false;
}

function altermsg(tips_name)
{
	if(req.readyState == 4)
	{
		if (req.status == 200)
		{
			clearInterval(INT);
			MM_findObj(tips_name).innerHTML = '';
			var result = req.responseText;
			parse_result(result);
			MM_findObj("do").disabled = false;
			ajax_ready = true;
		}
	}
}

function parse_result( s )
{
	if( s.length <= 0 ) return '';

	var text_link_arr = new Array();
	var img_link_arr = new Array();
	var link = s.split("\r\n");
	var cnt_text = 0;
	var cnt_img = 0;

	for(var i=0; i<link.length; i++)
	{
		if( link[i].match((new RegExp("^{","ig"))) )
		{
			var json_obj = JSON.parse(link[i]);
			continue;
		}
		if( link[i].indexOf('<img') > 0 || link[i].indexOf('<IMG') > 0)
		{
			img_link_arr[cnt_img++] = link[i];
		}
		else
		{
			text_link_arr[cnt_text++] = link[i];
		}
	}

	if( text_link_arr.length > 1 )
	{
		if( typeof json_obj == 'object' )
		{
			if( parseInt(json_obj.is_preview) == 1 )
			{
				MM_findObj('tips').innerHTML = tips_nav('提示：此网站没有被收录，是否现在<a href="site_add.php?uri='+json_obj.uri+'">添加网站</a>享受更多个性个功能？', 480);
			}
			MM_findObj('status').innerHTML = json_obj.status;
		}

		show_text_link( text_link_arr );
		show_img_link( img_link_arr );
	}
	else
	{
		if( link.length <= 2 )
		{
			MM_findObj('tips').innerHTML = tips_nav( '提示：没有找到合符规则的内容。' + s );
			return false;
		}
	}

	return true;
}

function show_text_link( s )
{
	var html='<div class="tilelist"><ul>';
	var tips='';
	for(var i=0; i<s.length; i++)
	{
		var pattern = new RegExp('^[0-9|]{1,}$','gi');
		if( s[i].match(pattern) )
		{
			var t = s[i].split('|');
			html +=  '</ul></div>' + '<div id="L_'+t[0]+'_'+t[1]+'">'
				+ '<div onMouseOver="this.style.backgroundColor=\'#CECEFF\';"  onMouseOut="this.style.backgroundColor=\'#FFFFFF\';" style="margin:0px;float:right;text-align:right;" id="A_'+t[0]+'_'+t[1]+'">'
				+ '<!-- <a href="#">↑顶端</a>--></div><div style="clear:both;"></div>'
				+'<div onMouseOver="this.style.backgroundColor=\'#CECEFF\';"  onMouseOut="this.style.backgroundColor=\'#FFFFFF\';" style="margin:0px;float:right;text-align:right;" id="A_'+t[0]+'_'+t[1]+'">'
				+ ' <a href="#" onclick="javascript:hide_this_cate(\'A_'+t[0]+'_'+t[1]+'\',' + t[0] + ',' + t[1] + ');return false;">隐藏此类</a></div>'
				+ '<div><ul>';
		}
		else
		{
			if(s[i].match((new RegExp('^<','ig'))))
			{
				if( i > 0 && i % 10 == 0 )
				{
					html += '<p style="color:#CCCCCC">-------------------------------------------------------</p>';
				}
				html += '<li>' + s[i] + '</li>';
			}
			else
			{
				tips += s[i];
			}
		}
	}
	html += '</ul></div>';
	//html += '<div>' + tips + '</div>';
	MM_findObj("info").innerHTML = html;
}

function show_img_link( s )
{
	var html='<div style="text-align:center;margin:0 auto;">';
	for(var i=0; i<s.length; i++)
	{
		html += '<div style="text-align:center;margin:5 auto 5 auto;padding:3;">' + s[i] + '</div><div style="clear:both;"></div>';
		if( i > 20 )
		{
			break;	//性能问题,过多的图不显示
		}
	}
	html += '</div>';
	MM_findObj("img").innerHTML = html;
}

var INT;
function loading_animation(tips_name)
{
	if( req && ! ajax_ready )
	{
		alert('上一次请求仍在处理中，请稍等。\r\n如果长时间没有回应，请刷新整个页面后重试。');
		return false;
	}
	else
	{
		if(INT)
		{
			clearInterval(INT);
		}
		INT = setInterval("loading_animation_cross('"+tips_name+"')",60);
		return true;
	}
}

var loading_animation_cnt = 0;
function loading_animation_text(tips_name)
{
	var text = 'loading';
	var text_suffix = '...';

	MM_findObj(tips_name).innerHTML = tips(text  + text_suffix.substr(0, loading_animation_cnt));

	loading_animation_cnt += 1;
	if( loading_animation_cnt > text_suffix.length ) loading_animation_cnt = 0;
}

function loading_animation_cross(tips_name)
{
	var text = '处理中，请稍候';
	var text_suffix = '-\\|/';

	MM_findObj(tips_name).innerHTML = tips(text  + ' ' + text_suffix.substr(loading_animation_cnt, 1 ) ) ;

	loading_animation_cnt += 1;
	if( loading_animation_cnt >= text_suffix.length ) loading_animation_cnt = 0;
}

function tips(s,width)
{
	if(!width)
	{
		width = calc_width(s);
	}

	return '<div style="color:#ff0000;background-color:#FFFF00;width:'+width+'px;text-align:center;border:1px dotted blue;padding:3px;white-space:nowrap;margin:0 auto 0 auto;">'+s+ '</div><div style="clear:both;"></div>';
}

function tips_nav(s,width)
{
	if( ! width )
	{
		width = calc_width(s);
	}

	return '<div style="color:#ff0000;background-color:#FCFCC2;width:'+width+'px;text-align:left;margin:10px;border:0px;padding:5px;white-space:nowrap;font-size:10.5pt;">'+s+ '</div><div style="clear:both;"></div><div style="clear:both;"></div>';
}

function calc_width(s)
{
	var han_cnt=0;
	var eng_cnt=0;
	var pattern = new RegExp("[\u4e00-\u9fa5]","ig");
	var width=0;

	for(var i=0; i<s.length; i++)
	{
		if( s.substr(i,1).match(pattern) )
		{
			han_cnt++;
		}
		else
		{
			eng_cnt++
		}
	}
	width = han_cnt*15+eng_cnt*6;

	return width;
}